//
//  WSTaskController.m
//  CharonTVProject
//
//  Created by karong on 2023/7/31.
//

#import "WSTaskController.h"
#import "WSTaskRequest.h"
#import "WSTask.h"
#import "WSSign.h"
#import "WSTaskCell.h"
#import "WSSignCell.h"
#import "CTMediator+WSTaskAction.h"
#import "AppDelegate.h"
#import "CTMediator+WSUserCenterAction.h"
#import "WSDiscoverController.h"
#import "WSADManager.h"

@interface WSTaskController () <WSTaskcharonTV_CellDelegate, WSSigncharonTV_CellDelegate, WSADManagerDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;

@property (nonatomic, strong) WSTaskData * taskData;
@property (nonatomic, strong) WSSignData * signData;
@end

@implementation WSTaskController

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self defaultcharonTV_Request];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [WSADManager shared].delegate = self;
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"fffdbf"];
}

- (void)styleDidChange {
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"fffdbf"];
}

- (void)defaultcharonTV_Request {
    [self loadSignListcharonTV_Request];
    [self loadTaskListcharonTV_Request];
}

- (void)loadSignListcharonTV_Request {
    [WSTaskRequest loadcharonTV_SignListWithSuccess:^(id obj) {
        self.signData = obj;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)loadTaskListcharonTV_Request {
    [WSTaskRequest loadcharonTV_TaskListWithSuccess:^(id obj) {
        self.taskData = obj;
        [self.listView reloadData];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)signcharonTV_Request {
    [WSTaskRequest signcharonTV_WithSuccess:^{
        WSSign * sign = self.signData.charonTV_items[self.signData.charonTV_sign_days];
        self.signData.charonTV_is_sign_today = 1;
        self.signData.charonTV_sign_days ++;
        [self.listView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
        WSTask * charonTV_task = [WSTask new];
        charonTV_task.charonTV_coins = sign.charonTV_coins;
        [self showcharonTV_ReceiveSuccessAlter:charonTV_task];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)signcharonTV_DoubleRequest {
    [WSTaskRequest signcharonTV_DoubleWithSuccess:^{
        [self.listView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]];
        WSTask * charonTV_task = [WSTask new];
        charonTV_task.charonTV_task_identify = -1;
        WSSign * sign = self.signData.charonTV_items[self.signData.charonTV_sign_days];
        charonTV_task.charonTV_coins = sign.charonTV_coins;
        [self showcharonTV_ReceiveSuccessAlter:charonTV_task];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)receivecharonTV_Request:(WSTask *)task {
    [WSTaskRequest receivecharonTV_WithTask_id:task.charonTV_id success:^{
        [self defaultcharonTV_Request];
        [self showcharonTV_ReceiveSuccessAlter:task];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
    }];
}

- (void)showcharonTV_ReceiveSuccessAlter:(WSTask *)task {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_ReceiveSuccessWithTask:task];
    target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    target.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:target animated:YES completion:nil];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section == 0 && self.signData) {
        count = 1;
    }
    else if (section == 1 && self.taskData.charonTV_newbie_task.count > 0) {
        count = 1;
    }
    else if (section == 2 && self.taskData.charonTV_day_task.count > 0) {
        count = 1;
    }
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSTaskCell * cell;
    if (indexPath.section == 0) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSignCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.signData];
        
    }
    else if (indexPath.section == 1) {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSTaskCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.taskData.charonTV_day_task type:0];

    }
    else {
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSTaskCell" forIndexPath:indexPath];
        [cell setDataWithObject:self.taskData.charonTV_newbie_task type:1];

    }
    cell.delegate = self;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth - 12 - 12;
    if (indexPath.section == 0 && self.signData.charonTV_items.count > 0) {
        size.width = WSScreenWidth;
        size.height = 550;
    }
    else if (indexPath.section == 1 && self.taskData.charonTV_newbie_task.count > 0) {
        size.height = 25.5 + self.taskData.charonTV_newbie_task.count * (46 + 26) + 26;
    }
    else {
        size.height = 25.5 + self.taskData.charonTV_day_task.count * (46 + 26) + 26;
    }
    
    return size;
}

#pragma mark WSTaskCellDelegate
- (void)taskcharonTV_CellTurnActionWithType:(NSInteger)type {
    NSTimer * realTime;
    [realTime setFireDate:[NSDate distantFuture]];
    //跳转类型 (0-无跳转 1-VIP充值 2-书币充值 10-首页推荐 11-分类 12-我的个人信息 14-书城 16 绑定三方)
    switch (type) {
        case 1:
        {
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:0];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 2:
        {
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PurchaseVCWithIndex:1];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        case 10:
        {
            [self.tabBarController setSelectedIndex:0];
        }
            break;
        case 12:
        {
            if (appUserCenter.isLogin) {
                UIViewController *userInfoVC = [[CTMediator sharedInstance]ct_showcharonTV_UserInfoWithUser:appUserCenter];
                [self.navigationController pushViewController:userInfoVC animated:YES];
            }
            else {
                [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
            }
        }
            break;
        case 16:
        {
            UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_Account];
            [self.navigationController pushViewController:target animated:YES];
        }
            break;
        default:
            break;
    }
}

-(void)taskcharonTV_CellReceiveActionWithObject:(WSTask *)object {
    //領取
    [self receivecharonTV_Request:object];
}

#pragma mark WSSigncharonTV_CellDelegate
- (void)signcharonTV_CellSignAction {
    [self signcharonTV_Request];
}

- (void)signcharonTV_CellRoleAction {
    UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_RoleAlter];
    target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    target.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:target animated:YES completion:nil];
}

#pragma mark WSADManagerDelegate
- (void)rewardVideoPlayComplete:(WSADManager *)manager {
    [self signcharonTV_DoubleRequest];
}
@end
